package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.RemarkQuery;
import cn.wolfcode.luowowo.article.query.ScenicDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IScenicRedisService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.common.util.AJaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("scenic")
public class ScenicController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicRegionService scenicRegionService;
    @Reference
    private IScenicDetailService scenicDetailService;
    @Reference
    private IScenicTagService scenicTagService;
    @Reference
    private IScenicThemeService scenicThemeService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IRemarkService remarkService;
    @Reference
    private IScenicRedisService scenicRedisService;


    @RequestMapping("")
    public String list(Long destId, Model model){

        // toasts
        List<Destination> toasts = destinationService.getToasts(destId);
        model.addAttribute("toasts",toasts.subList(0,toasts.size()-1));
        //dest
        Destination dest = destinationService.getById(destId);
        model.addAttribute("dest",dest);
        //ScenicRegion
        List<ScenicRegion> regions =scenicRegionService.listByDestId(destId);
        model.addAttribute("regions",regions);

        //热门景点
        List<ScenicDetail> details =scenicDetailService.listHot();
        if(details.size()>=10) {
            model.addAttribute("details0", details.get(0));
            model.addAttribute("details1", details.get(1));
            model.addAttribute("details2", details.get(2));
            model.addAttribute("details3", details.get(3));
            model.addAttribute("details4", details.get(4));
            model.addAttribute("details5", details.get(5));
            model.addAttribute("details6", details.get(6));
            model.addAttribute("details7", details.get(7));
            model.addAttribute("details8", details.get(8));
            model.addAttribute("details9", details.get(9));
        }

        //查询标签
       List<ScenicTag> tags= scenicTagService.listTag(destId);
        model.addAttribute("tags",tags);
        //查询所有景点
        List<ScenicDetail> allDetail = scenicDetailService.listbyDest();
        model.addAttribute("allDetail",allDetail);
        return "scenic/list";

        //查询mongodb中的内容


    }

    @RequestMapping("all")
    public String all(ScenicDetailQuery qo,Model model){

        //全部景点
       PageInfo pageInfo =scenicDetailService.query(qo);
       model.addAttribute("destId",qo.getDestId());
        model.addAttribute("pageInfo",pageInfo);
        return "scenic/scenicTpl";
    }

    @RequestMapping("detail")
    public Object detail(RemarkQuery qo, Model model){
        // toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts",toasts.subList(2,toasts.size()-1));
        //dest
        Destination dest = destinationService.getById(qo.getDestId());
        model.addAttribute("dest",dest);

        //查询单个ScenicDetail
        if(qo.getRegionId()!=null){
            ScenicRegion region =scenicRegionService.getRegion(qo.getRegionId());
            //内部景点
            Long[] refIds = region.getRefIds();
            List<ScenicDetail> details =scenicDetailService.listInside(refIds);
            model.addAttribute("details",details);


            ScenicDetail detail =scenicDetailService.getDetail(region.getId());
            model.addAttribute("detail",detail);
            //评论回复
//            List<ScenicComment> comments =scenicCommentService.selectComment(region.getId());
//            model.addAttribute("comments",comments);

        }else if(qo.getDestId()!=null){
            ScenicDetail detail = scenicDetailService.getDetail(qo.getDetailId());
            model.addAttribute("detail",detail);
            //评论回复
//            List<ScenicComment> comments =scenicCommentService.selectComment(qo.getDetailId());
//            model.addAttribute("comments",comments);
        }

    //====todo 下面的功能有些问题



        //蜂蜂点评
        //查询分类  例如，有图（132）
        List<Map<String,Object>> themes=scenicThemeService.getClassify();
        model.addAttribute("themes",themes);


        //查询点评
        PageInfo pageInfo =remarkService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "scenic/detail";



    }

    @RequestMapping("input")
    public String input(Long detailId,Long destId,Model model, @UserParam UserInfo userInfo){
//        if(userInfo == null){
//            AJaxResult aJaxResult = new AJaxResult(false,"请登录后再点评!");
//            aJaxResult.setCode(102);
//            return aJaxResult;
//        }
        model.addAttribute("destId",destId);
        model.addAttribute("detailId",detailId);
        model.addAttribute("detail",scenicDetailService.getDetail(detailId));
        return "scenic/input";
    }

    @RequestMapping("save")
    @ResponseBody
    public Object save(Remark remark,@UserParam UserInfo userInfo){
        remark.setUser(userInfo);//把用户信息存进去
        remarkService.save(remark);
        return new AJaxResult();
    }

//    @RequestMapping("commentAdd")
//    public Object commentAdd(ScenicComment comment,Model model,@UserParam UserInfo userInfo){
//        //用户信息
//        comment.setUserId(userInfo.getId());
//        comment.setCity(userInfo.getCity());
//        comment.setHeadUrl(userInfo.getHeadImgUrl());
//        comment.setLevel(userInfo.getLevel());
//        comment.setUsername(userInfo.getNickname());
//
//        model.addAttribute("c",scenicCommentService.addComment(comment));
//
//        return "scenic/commentTpl";
//
//    }
    @RequestMapping("commentAdd")
    public Object commentAdd(ScenicComment comment,Model model,@UserParam UserInfo userInfo){
        //用户信息
        comment.setUserId(userInfo.getId());
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUsername(userInfo.getNickname());

        model.addAttribute("c",scenicCommentService.addComment(comment));

        return "scenic/commentTpl";

    }

    //评论点赞
    @RequestMapping("favornum")
    @ResponseBody
    public Object favornum(Long remarkId,@UserParam UserInfo userInfo){
        remarkService.favornum(remarkId,userInfo.getId());
        return new AJaxResult();
    }
    //=================================

    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long detailId,@UserParam UserInfo userInfo){
        if(userInfo == null){
            AJaxResult aJaxResult = new AJaxResult(false,"请登录后再收藏!");
            aJaxResult.setCode(102);
            return aJaxResult;
        }
       boolean ret = scenicRedisService.favor(detailId,userInfo.getId());
        return new AJaxResult(ret, "").addData(scenicRedisService.getStatisVo(detailId));
    }
}
